import { SubjectComponent } from "../../../core/subject";
import { Random } from "../../../core/utils";
import CCCharacter from "../cc_game/cc_character";
import { Character, CharacterConfig } from "../game/character";
import { GameUtils } from "../game_utils";

const { ccclass, property } = cc._decorator;

export const EYE_COUNT = 18;
export const MOUTH_COUNT = 9;
// eye 1-8
// mouth 1-6
@ccclass
export default class EggyReadyItem extends SubjectComponent {
    @property(cc.Node)
    charRoot: cc.Node = null;
    ccChar: CCCharacter = null;  

    char: Character = null;
    onLoad() {
        this.ccChar = GameUtils.InstantiateCCGameCom("eggy", CCCharacter);
        this.charRoot.addChild(this.ccChar.node);
        if (!this.char) {
            this.char = new Character();
            let conf = new CharacterConfig();
            conf.color = Random.range(0, this.ccChar.colors.length);
            conf.eye = Random.range(1, 9);
            conf.mouth = Random.range(1, 7);
            conf.ai = 0; 
            CharacterConfig.AssignUniqueObject(this.char, conf);
        }
    }

    start() {
        this.ccChar.setData(this.char);
    }

    onClickDel() {
        this.emit("del", this);
        this.node.destroy();
    }

    onClickColor(evt, val) {
        let color = this.char.data.color;
        switch (val) {
            case "<":
                color = color - 1;
                if (color < 0) {
                    color = this.ccChar.colors.length - 1;
                }
                break;
            case ">":
                color = color + 1;
                if (color > this.ccChar.colors.length - 1) {
                    color = 0;
                }
                break;
        }
        this.char.data.color = color;
    }
    onClickEye(evt, val) {
        let eye = this.char.data.eye;
        switch (val) {
            case "<":
                eye = eye - 1;
                if (eye < 1) {
                    eye = EYE_COUNT;
                }
                break;
            case ">":
                eye = eye + 1;
                if (eye > EYE_COUNT) {
                    eye = 1;
                }
                break;
        }
        this.char.data.eye = eye;
    }
    onClickMouth(evt, val) {
        let mouth = this.char.data.mouth;
        switch (val) {
            case "<":
                mouth = mouth - 1;
                if (mouth < 1) {
                    mouth = MOUTH_COUNT;
                }
                break;
            case ">":
                mouth = mouth + 1;
                if (mouth > MOUTH_COUNT) {
                    mouth = 1;
                }
                break;
        }
        this.char.data.mouth = mouth;
    }
    onClickRandom() {
        this.char.data.color = Random.range(0, this.ccChar.colors.length - 1);
        this.char.data.eye = Random.range(1, EYE_COUNT);
        this.char.data.mouth = Random.range(1, MOUTH_COUNT);
    }
};